//
//  CXHUBiMAXConfigAdapter.m
//  CXHAdSDK-UBiMAX
//
//  Created by 陈坤 on 2025/7/27.
//

#import "CXHUBiMAXConfigAdapter.h"
#import <UBiMAXAdSDK/UBiMAXAdSDK-umbrella.h>
#import <CXHAdSDK-Core/CXHAdSDK.h>
#import <CXHAdSDK-Kit/CXHUtilsKit.h>

@interface CXHUBiMAXConfigAdapter () <UMTCustomConfigAdapter>

@end

@implementation CXHUBiMAXConfigAdapter

/// 该自定义adapter是基于哪个版本实现的，填写编写时的最新值即可，UMT会根据该值进行兼容处理
- (NSString *)basedOnCustomAdapterVersion {
    return nil;
}

/// adn初始化方法
/// @param initConfig 初始化配置，包括appid、appkey基本信息和部分用户传递配置
- (void)initializeAdapterWithConfiguration:(UMTAdnSDKInitConfig *_Nullable)initConfig completed:(void (^)(BOOL, NSError *))handler {
    NSString *customParamJsonStr = initConfig.customExtParamsStr;
    NSString *domain = @"";
    if (customParamJsonStr) {
        NSData *data = [customParamJsonStr dataUsingEncoding:NSUTF8StringEncoding];
        if (data) {
            NSError *error = nil;
            NSDictionary *paramDic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:&error];
            if (!error && paramDic) {
                domain = [paramDic CXH_objectOrNilForKey:@"domain"];
            }
        }
    }
    
    NSString *appId = initConfig.appID;
    [CXHAdSDK initWithAppId:appId requestDomain:domain completionBlock:^(NSError * _Nullable error) {
        if (!handler) {
            return;
        }
        if (error) {
            NSError *err = [[NSError alloc] initWithDomain:@"com.ubixai.umtd.cxh"
                                                      code:404
                                                  userInfo:@{NSLocalizedDescriptionKey: @"cxhsdk init Failed"}];
            handler(NO, err);
        } else {
            handler(true, error);
        }
    }];
}

/// adapter的版本号
- (NSString *_Nonnull)adapterVersion {
    return @"1.0.0";
}

/// adn的版本号
- (NSString *_Nonnull)adnSdkVersion {
    return [CXHAdSDK getSDKVersion];
}

/// 隐私权限更新，用户更新隐私配置时触发，初始化方法调用前一定会触发一次
- (void)didRequestAdPrivacyConfigUpdate:(NSDictionary *)config {
    
}

@end
